﻿using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.MySQL;
using Volo.Abp.EventBus;
using Volo.Abp.EventBus.Abstractions;
using Volo.Abp.Modularity;
using Volo.Abp.Uow;
using WorkFlowCore.Authorization;
using WorkFlowCore.Common.SimplePluginLoaders;
using WorkFlowCore.Conditions;
using WorkFlowCore.Framework.Authorization;
using WorkFlowCore.Framework.Conditions;
using WorkFlowCore.Framework.EventHandlers;
using WorkFlowCore.Framework.Repositories4EF;
using WorkFlowCore.Framework.UserSelectors;
using WorkFlowCore.IRepositories;
using WorkFlowCore.UserSelectors;
using static WorkFlowCore.WorkFlowCoreService;

namespace WorkFlowCore.Framework
{
    [DependsOn(typeof(AbpEventBusModule))]
    [DependsOn(typeof(WorkFlowCoreModule))]
    [DependsOn(typeof(AbpEntityFrameworkCoreMySQLModule))]
    [DependsOn(typeof(AbpUnitOfWorkModule))]
    public  class WorkFlowCoreFrameworkModule :AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            base.ConfigureServices(context);

            var services = context.Services;

            services.AddScoped(typeof(IBasicRepository<,>), typeof(BasicRepository4EF<,>));
            services.AddScoped(typeof(IBasicRepository<>), typeof(BasicRepository4EF<>));
            services.AddScoped(typeof(IWorkStepRepository), typeof(WorkStepRepository4EF));
            services.AddScoped(typeof(IWorkTaskRepository), typeof(WorkTaskRepository4EF));



            services.AddAbpDbContext<WorkflowDbContext>(options =>
            {
                options.AddDefaultRepositories(includeAllEntities: true);
            });

            Configure<AbpDbContextOptions>(options =>
            {
                /* The main point to change your DBMS.
                 * See also WorkflowMigrationsDbContextFactory for EF Core tooling. */
                options.UseMySQL();
            });

          

            //services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            //事件处理
            services.AddScoped<SendTaskEventHandler>();
            services.AddScoped<TaskFinishedEventHandler>();
            services.AddScoped<AutoHandleStepsEventHandler>();

            var assembly = typeof(WorkFlowCoreFrameworkService).Assembly;
            

            //注册条件和选择器
            #region 注册条件和选择器
            try
            {
                //项目实现的
                UserSelectorManager.RegisterSelector(services, assembly);
                ConditionManager.Registercondition(services, assembly);


                //外部插件注册的
                services.LoadPlugins(Path.Combine(AppContext.BaseDirectory, "Plugins", "UserSelectors"), plugins =>
                {
                    UserSelectorManager.RegisterSelector(plugins.Select(p => p.Assembly).ToArray());

                });
                services.LoadPlugins(Path.Combine(AppContext.BaseDirectory, "Plugins", "Conditions"), plugins =>
                {
                    ConditionManager.Registercondition( plugins.Select(p => p.Assembly).ToArray());
                });
            }
            catch (Exception ex)
            {
                //
            }
            #endregion
        }

        public override void PostConfigureServices(ServiceConfigurationContext context)
        {
            base.PostConfigureServices(context);

            context.Services.Replace(new ServiceDescriptor(typeof(IWorkflowSession), typeof(DefaultSession), ServiceLifetime.Scoped));
        }
    }
}
